//
//  DFShareDemoController.m
//  DFKit
//
//  Created by rujia chen on 17/6/13.
//  Copyright © 2017年 chenrj90. All rights reserved.
//

#import "DFShareDemoController.h"
#import <DFKit/DFKitShare.h>

@interface DFShareDemoController ()<DFRouterPage, UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) NSArray <RACTuple *>*platforms;

@property (nonatomic, strong) UIPickerView *pickerView;

@property (nonatomic, strong) RACTuple *selectedPlatorm;

@end

@implementation DFShareDemoController

+ (void)load {
    [DFRouter registerURL:DF_DEMO_SHARE forViewController:[self class]];
}

- (NSString *)title {
    return @"分享";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.platforms = @[
                       RACTuplePack(@(DFSharePlatformTypeWechatSession), @"微信好友"),
                       RACTuplePack(@(DFSharePlatformTypeWechatTimeline), @"朋友圈"),
                       RACTuplePack(@(DFSharePlatformTypeQQ), @"QQ好友"),
                       RACTuplePack(@(DFSharePlatformTypeQzone), @"QQ空间"),
                       RACTuplePack(@(DFSharePlatformTypeSinaWeibo), @"新浪微博")];
    self.selectedPlatorm = self.platforms[0];
    
    [self.view addSubview:self.pickerView];
    [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.height.equalTo(@275);
    }];
    
    UIButton *button = DFMakeButton(nil, nil, nil, [UIFont systemFontOfSize:[UIFont systemFontSize]], UIButtonTypeSystem);
    [self.view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.pickerView.mas_bottom);
    }];
    
    [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self shareWebWithPlatform:[self.selectedPlatorm[0] integerValue]];
    }];
    
    RAC(self, selectedPlatorm) = [[self rac_signalForSelector:@selector(pickerView:didSelectRow:inComponent:) fromProtocol:@protocol(UIPickerViewDelegate)] map:^id _Nullable(RACTuple * _Nullable value) {
        NSInteger row = [value[1] integerValue];
        return self.platforms[row];
    }];
    
    [RACObserve(self, selectedPlatorm) subscribeNext:^(id  _Nullable x) {
        [button setTitle:x[1] forState:UIControlStateNormal];
    }];
}

- (void)shareWebWithPlatform:(DFSharePlatformType)type {
    [DFShareManager shareWebPage:@"https://www.rongzi.com"
                           title:@"title" desc:@"desc"
                      thumbImage:@"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2671252904,1920085140&fm=58"
                    platformType:type
                      completion:^(id result, NSError *error) {
                                    NSLog(@"ok result:%@ error:%@", result, error);
                                }];
}

#pragma mark - UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.platforms.count;
}

#pragma mark - UIPickerViewDelegate

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    RACTuple *tuple = self.platforms[row];
    return tuple[1];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
}

#pragma mark - getter

- (UIPickerView *)pickerView {
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] init];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
    }
    
    return _pickerView;
}

@end
